<template>
  <div class="todo-footer" v-show="AllTotal">
    <label>
      <input type="checkbox" :checked="isAll" @change="checkAll" />
    </label>
    <span v-show="AllTotal">
      <span>已完成{{ doneTotal }}</span>
      / 全部{{ AllTotal }}
    </span>
    <button class="btn btn-danger" @click="deleteAll">清除已完成任务</button>
  </div>
</template>

<script>
export default {
  name: 'MyFooter',
  props: ['todoList'],
  computed: {
    //计算已完成的todo的数量
    doneTotal() {
      // reduce用于计数,第一个参数是上一次return的值，第二个参数是当前的对象,数组有几个就会调用几次
      return this.todoList.reduce(
        (pre, todo) => pre + (todo.status ? 1 : 0),
        0
      );
    },
    AllTotal() {
      return this.todoList.length;
    },
    isAll() {
      return this.doneTotal === this.AllTotal && this.AllTotal > 0;
    },
  },
  methods: {
    checkAll(e) {
      // console.log(e.target.checked);
      this.$emit('checkAllTodo', e.target.checked);
    },
    deleteAll() {
      if (confirm('确定要清除吗?')) {
        this.$emit('deleteAllTodo');
      }
    },
  },
};
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>
